package com.jswlw.controller.admin.biz;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.jswlw.common.BaseException;
import com.jswlw.common.ResponseStatus;
import com.jswlw.controller.BaseController;
import com.jswlw.entity.biz.DistributionRecord;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.entity.biz.DistributorAmountRecord;
import com.jswlw.service.biz.DistributionRecordService;
import com.jswlw.service.biz.DistributorAmountRecordService;
import com.jswlw.service.biz.DistributorService;
import com.jswlw.web.manager.SessionAdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Objects;

@Controller
@RequestMapping("/admin/biz/distributionAmountRecord")
public class AdminDistributionAmountRecordController extends BaseController {

    @Autowired
    DistributorAmountRecordService service;
    @Autowired
    DistributorService distributorService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "biz/distributionAmountRecord/index";
    }

    /**
     * 广告列表
     *
     * @param
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody DistributorAmountRecord amountRecord) {
        if (Objects.nonNull(SessionAdminManager.getCurrentUser().getDistributor())) {
            amountRecord.setDistributorIds(SessionAdminManager.getCurrentUser().getDistributor().getChildrenIds());
        }
        Page<DistributorAmountRecord> page = service.queryPage(amountRecord);
        logger.info(JSON.toJSONString(page.getResult()));
        return pageResult(page.getResult(), page.getTotal());
    }

    /**
     * 新增广告
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("biz/distributionAmountRecord/edit");
        return modelAndView;
    }

    /**
     * 保存广告
     *
     * @param amountRecord
     * @param
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(DistributorAmountRecord amountRecord) {
        try {
            if (Objects.isNull(distributorService.find4Id(amountRecord.getDistributorId()))) {
                BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "经销商不存在");
            }
            if (Objects.nonNull(SessionAdminManager.getCurrentUser().getDistributor())) {
                if (SessionAdminManager.getCurrentUser().getDistributor().getId().equals(amountRecord.getDistributorId())) {
                    BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "不允许给自己充值");
                }
                Distributor distributor = distributorService.find4Id(amountRecord.getDistributorId());
                if (!distributor.getParentId().equals(SessionAdminManager.getCurrentUser().getDistributor().getId())) {
                    BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "只能给自己的下级代理商充值");
                }
            }
            //查询代理商是否是当前代理商的下级代理商

            amountRecord.setCreateTime(new Date());
            service.add(amountRecord);
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            if (e instanceof BaseException) {
                return this.failResult(((BaseException) e).getMessage());
            }
            return this.failResult("保存失败经销商不存在");
        }
    }
}
